package com.photologics.main;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

import com.photologics.logic.LogicHelper;

public class BackgroundMusic {
	private MediaPlayer mp;
	public BackgroundMusic(Context context){
		mp = MediaPlayer.create(context, R.raw.bgm);
		mp.setLooping(true);
	}
	public void onStart(){
		if(!LogicHelper.bgmMute)
			mp.start();
	}
	
	public void onPause(Context context){
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
		if (!taskInfo.isEmpty()) {
			ComponentName topActivity = taskInfo.get(0).topActivity;
			Log.d("event",
					"jo topActivity.getPackageName()="
							+ topActivity.getPackageName());
			Log.d("event",
					"jo context.getPackageName()=" + context.getPackageName());
			if (!topActivity.getPackageName().equals(context.getPackageName())) {
				mp.pause();
			}
		}
	}
	public void onPause(){
		mp.pause();
	}
	public void onStop(){
		mp.stop();
	}

}
